<template>
  <div>
    <!-- 从vuex中获取数据 -->
    <h4>当前求和为:{{ sum }}</h4>
    <h4>当前getters为{{ bigSum }}</h4>
    <!-- 用户选择的数字 -->
    <select v-model.number="select">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
    </select>
    <button @click="increment">+</button>
    <button @click="decrement">-</button>
    <button @click="incrementOdd">当前求和为奇数再加</button>
    <button @click="incrementAwait">等一等再加</button>
  </div>
</template>

<script>
import { mapState, mapGetters } from "vuex";
export default {
  name: "count",
  data() {
    return {
      select: 1,
    };
  },
  methods: {
    increment() {
      //派发action
      // this.$store.dispatch("increment", this.select);
      this.$store.commit("INCREMENT", this.select);
    },
    decrement() {
      // this.$store.dispatch("decrement", this.select);
      this.$store.commit("DECREMENT", this.select);
    },
    incrementOdd() {
      this.$store.dispatch("incrementOdd", this.select);
    },
    incrementAwait() {
      this.$store.dispatch("incrementAwait", this.select);
    },
  },
  computed: {
    //获取state中的数据每次都需要 $store.state.xxx
    //此时可以用辅助函数 mapstate
    //mapState可以把仓库中的state数据映射到组件实例对象身上
    // ...mapState({sum:'sum'}) 对象写法
    ...mapState(["sum"]), //数组写法
    //这样可以快速获取仓库数据

    //快速获取仓库中的getters
    ...mapGetters(["bigSum"]),
  },
};
</script>

<style scoped>
</style>